package cn.wit.sst.utils;

import java.util.List;

import org.springframework.stereotype.Component;

import cn.wit.sst.domain.College;
import cn.wit.sst.domain.Course;
import cn.wit.sst.domain.Major;

@Component("appCache")
public class AppCache extends LruCache<String, Object> {
	public static class CacheReadyEvent {}

	public static final String GROUP_COLLEGE = "db:college:";
	public static final String GROUP_MAJOR = "db:major:";
	public static final String GROUP_COURSE = "db:course:";

	private static final int MAX_SIZE = 10 * 1024 * 1024;

	public AppCache() {
		super(MAX_SIZE);
	}

	public Major getMajor(Long id) {
		return (Major) get(GROUP_MAJOR + id);
	}

	public void putMajor(Long id, Major m) {
		put(GROUP_MAJOR + id, m);
	}

	public College getCollege(Long id) {
		return (College) get(GROUP_COLLEGE + id);
	}

	public void putCollege(Long id, College c) {
		put(GROUP_COLLEGE + id, c);
	}

	public void putCourse(Long id, Course c) {
		put(GROUP_COURSE + id, c);
	}

	public Course getCourse(Long id) {
		return (Course) get(GROUP_COURSE + id);
	}

	@SuppressWarnings("unchecked")
	public List<Course> getCourses() {
		return (List<Course>) get(GROUP_COURSE);
	}

	public void putCourses(List<Course> courses) {
		put(GROUP_COURSE, courses);
	}

	@SuppressWarnings("unchecked")
	public List<Major> getMajors() {
		return (List<Major>) get(GROUP_MAJOR);
	}

	public void putMajors(List<Major> majors) {
		put(GROUP_MAJOR, majors);
	}

	@SuppressWarnings("unchecked")
	public List<College> getColleges() {
		return (List<College>) get(GROUP_COLLEGE);
	}

	public void putColleges(List<College> colleges) {
		put(GROUP_COLLEGE, colleges);
	}
}
